﻿Imports System.Data.SqlClient

Public Class frmPrecios
    Dim P As New clsPrecios
    Private Sub frmPrecios_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CargaPrecios()

    End Sub
    Sub CargaPrecios()
        dgPrecios.Rows.Clear()

        Dim cnConn As New sqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "Select IdRuta, Nombre from Rutas ORDER BY IdRuta"
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Dim Precio As Double = 0
        Dim Abre As String = ""

        dgPrecios.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgPrecios.Columns(2).DefaultCellStyle.Format = "N2"

        Do While rdBuscar.Read()
            Precio = BuscarDblDatoCitra("PrecioVueltas", "Precio", "IdRuta=" & rdBuscar("IdRuta"))
            Abre = BuscarStrDatoCitra("PrecioVueltas", "Abre", "IdRuta=" & rdBuscar("IdRuta"))
            dgPrecios.Rows.Add(rdBuscar("IdRuta"), rdBuscar("Nombre").ToString.Trim, Precio, Abre.Trim)
        Loop
    End Sub

    Private Sub RadButton4_Click(sender As System.Object, e As System.EventArgs) Handles RadButton4.Click
        Dim i As Integer = 0
        Dim precio As Double
        Dim aux As String

        Call SetSqlCitra("DELETE FROM PrecioVueltas")

        For i = 0 To dgPrecios.Rows.Count - 1
            aux = dgPrecios.Rows(i).Cells(2).Value
            If Len(aux) = 0 Then
                precio = 0
            Else
                precio = dgPrecios.Rows(i).Cells(2).Value
            End If

            P.IdRuta = dgPrecios.Rows(i).Cells(0).Value
            P.Precio = precio
            P.Abre = dgPrecios.Rows(i).Cells(3).Value.ToString.Trim
            P.Insertar()
        Next
        Close()

    End Sub
End Class